<!DOCTYPE HTML><html lang="en">
<HEAD>

<meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2013. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >

<meta charset="utf-8">
<LINK REL="STYLESHEET" HREF="../book.css" TYPE="text/css">
<TITLE>Custom Layouts</TITLE>

<link rel="stylesheet" type="text/css" HREF="../book.css">
</HEAD>
<BODY>

<H3>Custom layouts</H3>

<P>Occasionally you may need to write your own custom
<a href="../reference/api/org/eclipse/swt/widgets/Layout.html"><strong>Layout</strong></a>
class. This is most appropriate when you have a complex layout that is used
in many different places in your application.   Note that unless you are
writing a very generic layout that will be used by several
<strong><a href="../reference/api/org/eclipse/swt/widgets/Composite.html">Composite</a></strong>
widgets, it is sometimes simpler and easier to calculate sizes and position
children in a resize listener.</P>

<P>Layouts are responsible for implementing two methods:</P>

<ul>
  <li><strong>computeSize(...)</strong> calculates the width and height of a
      rectangle that encloses all of the composite's children once they have
      been sized and placed according to the layout algorithm. The hint
      parameters allow the width and/or height to be constrained.  For example,
      a layout may choose to grow in one dimension if constrained in another.</li>
  <li><strong>layout(...)</strong> positions and sizes the composite's children.
      A layout can choose to cache layout-related information, such as the
      preferred extent of each of the children. The <strong>flushCache</strong>
      parameter tells the <a href="../reference/api/org/eclipse/swt/widgets/Layout.html"><strong>Layout</strong></a>
      to flush cached data, which is necessary when other factors besides the
      size of the composite have changed, such as the creation or removal of
      children, or a change in the widget's font.</li>
</ul>

<p>A third method, <strong>flushCache(...)</strong>, can be optionally
implemented to clear any cached data associated with a specific control.
Often, the computeSize() method of a widget can be expensive, and so layouts
can cache results to improve performance.</p>

<p>Further discussion of custom layouts can be found in
<a href="http://www.eclipse.org/articles/Article-Understanding-Layouts/Understanding-Layouts.htm">Understanding
layouts in SWT</a>.</p>

</BODY>
</HTML>